<template>
  <div>
    <el-dialog
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :title="title"
      width="500px"
      @close="close"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        size="small"
        label-width="80px"
        label-suffix="："
      >
        <el-form-item label="价格" prop="price">
          <el-input
            v-model="form.price"
            clearable
            maxlength="8"
          >
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" round @click="hide">取 消</el-button>
        <el-button size="small" round type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { valid2Decimal } from '@/utils/validator'
export default {
  data() {
    return {
      //  校验字段
      rules: {
        price: [
          {
            required: true,
            validator: valid2Decimal,
            trigger: ['blur', 'change']
          }
        ]
      },
      // 配置
      resolve: null,
      reject: null,
      // 状态
      dialogVisible: null,
      // 数据
      title: null,
      form: {
        price: ''
      }
    }
  },
  methods: {
    /**
     * 弹框相关
     */
    open(title) {
      this.title = title
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    close() {
      this.form = {
        price: ''
      }
      this.$refs.form.resetFields()
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    /**
     * 数据组
     */

    /**
     * 按钮组
     */
    // 确 定
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.resolve && this.resolve(this.form)
		  setTimeout(() => {
			  this.hide()
		  }, 100)
        }
      })
    }

    /**
     * 状态组
     */

    /**
     * 方法组
     */
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
